﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Sys.Interfaces;
using AMS.DataAccess.Sys.BO;
using System.Text;
using AMS.DataAccess.Trademark.BO;

public partial class RecvFile_ucInstructionRecvFileDetail : UCBase, IRecvFileEditorCtrl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddClientIDsToClient();
        if (!IsPostBack)
        {
            txtRFDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            BindDropdownList();
        }
        else
        {
            LoadPostBackData();
        }

        AMSCKEditor.RegisterCKEditor(this.Page);
        ltrEditor.Text = AMSCKEditor.GetCreateCKEditorJs(txtContent.ClientID);
    }

    public bool IsInEditMode
    {
        get { return ViewState["IsInEditMode"] == null ? false : (bool)ViewState["IsInEditMode"]; }
        set { ViewState["IsInEditMode"] = value; }
    }

    void BindDropdownList()
    {
        IUserManager userMgr = ManagerFactory.CreateUserManager();
        List<UserInfo> users = userMgr.GetActiveUsers(CallContext);
        drpAssignedUser.DataSource = users;
        drpAssignedUser.DataTextField = "DisplayName";
        drpAssignedUser.DataValueField = "AID";
        drpAssignedUser.DataBind();
    }

    void AddClientIDsToClient()
    {
        StringBuilder script = new StringBuilder();
        script.AppendFormat("var id_txtRFName = '{0}';\r\n", txtRFName.ClientID);
        script.AppendFormat("var id_hfRFIndex = '{0}';\r\n", hfRFIndex.ClientID);

        Page.ClientScript.RegisterStartupScript(Page.GetType(),
            "99C7983D-ADCD-49A1-97A2-757E9FF32729",
            script.ToString(),
            true);
    }

    void LoadPostBackData()
    {
        txtRFName.Text = Request[txtRFName.UniqueID];
        txtCustomer.Text = Request[txtCustomer.UniqueID];
        hfCustomerID.Value = Request[hfCustomerID.UniqueID];
        hfRFIndex.Value = Request[hfRFIndex.UniqueID];
    }

    protected void drpAssignedUser_DataBound(object sender, EventArgs e)
    {
        if (drpAssignedUser.Items.Count > 0)
        {
            drpAssignedUser.Items.Insert(0, new ListItem("", "0"));
        }
    }

    StringBuilder sbMsg;
    public string ErrorMessage
    {
        get
        {
            if (sbMsg != null) { return sbMsg.ToString(); }
            else return string.Empty;
        }
    }

    /// <summary>
    /// 任务分配用户
    /// </summary>
    public string TaskAssignedUserID
    {
        get { return drpAssignedUser.SelectedValue; }
    }

    public bool ValidateFormData()
    {
        bool bResult = true;
        if (sbMsg == null)
        {
            sbMsg = new StringBuilder();
        }

        if (string.IsNullOrEmpty(hfCustomerID.Value))
        {
            sbMsg.Append("请选择或新建客户; ");
            bResult = false;
        }
        if (string.IsNullOrEmpty(txtRFDate.Text))
        {
            sbMsg.Append("请选择收文日期; ");
            bResult = false;
        }

        return bResult;
    }


    /// <summary>
    /// 获取收文信息
    /// </summary>
    /// <param name="rf"></param>
    /// <returns></returns>
    public bool GetRecvFileInfo(RecvFileInfo rf)
    {
        if (!ValidateFormData())
        {
            return false;
        }

        rf.RFName = txtRFName.Text;
        rf.CustomerID = int.Parse(hfCustomerID.Value);
        rf.RFContent = txtContent.Text;
        rf.RFIndex = hfRFIndex.Value;
        rf.RFName = txtRFName.Text;
        rf.Source = "C";

        if (!string.IsNullOrEmpty(txtRFDate.Text))
        {
            rf.RecvDate = DateTime.Parse(txtRFDate.Text);
        }
        if (!string.IsNullOrEmpty(txtSentFileDate.Text))
        {
            rf.FileSentDate = DateTime.Parse(txtSentFileDate.Text);
        }
        if (!string.IsNullOrEmpty(txtAnswerDate.Text))
        {
            rf.AnswerDate = Convert.ToDateTime(txtAnswerDate.Text);
        }

        return true;
    }

    public void EditRecvFile(RecvFileInfo rf)
    {
        txtAnswerDate.Text = Utils.DateTimeToString(rf.AnswerDate);
        txtRFDate.Text = Utils.DateTimeToString(rf.RecvDate);
        txtSentFileDate.Text = Utils.DateTimeToString(rf.FileSentDate);
        txtContent.Text = rf.RFContent;
        txtCustomer.Text = rf.CustomerName;
        txtRFName.Text = rf.RFName;
        hfCustomerID.Value = rf.CustomerID.ToString();
        hfRFIndex.Value = rf.RFIndex;
        IsInEditMode = true;
    }

}
